package com.atguigu.spring.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class LoggerAspect {

    public void beforeAdviceMethod(JoinPoint joinPoint){
        //joinPoint：连接点，通过这个对象，可以获取连接点的各种信息
        //获取连接点对应方法的签名（声明）信息
        Signature signature = joinPoint.getSignature();
        //获取连接点对应方法的参数
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect:前置通知"+" 方法名:"+signature.getName()+" 参数信息："+ Arrays.toString(args));
    }

    public void afterAdviceMethod(JoinPoint joinPoint){
        Signature signature = joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        System.out.println("LoggerAspect:后置通知 "+"方法名："+signature.getName()+" 执行完毕");
    }


    public void afterReturningAdviceMethod(JoinPoint joinPoint,Object result){
        Signature signature = joinPoint.getSignature();
        System.out.println("LoggerAspect：返回通知"+" 方法名："+signature.getName()+" 结果:"+result);

    }

    public void afterThrowingAdviceMethod(JoinPoint joinPoint,Throwable exception){
        Signature signature = joinPoint.getSignature();
        System.out.println("LoggerAspect：异常通知"+" 方法名："+signature.getName()+" 异常："+exception);

    }


    public Object aroundAdviceMethod(ProceedingJoinPoint proceedingJoinPoint){
        Signature signature = proceedingJoinPoint.getSignature();
        Object[] args = proceedingJoinPoint.getArgs();
        Object result = null;
        try{
            //前置通知
            System.out.println("LoggerAspect:环绕通知->前置通知"+" 方法名:"+signature.getName()+" 参数信息："+ Arrays.toString(args));
            //表示目标对象方法的执行
            result = proceedingJoinPoint.proceed();
            System.out.println("LoggerAspect:环绕通知->返回通知"+" 方法名："+signature.getName()+" 结果:"+result);

        }catch (Throwable throwable){
            throwable.printStackTrace();
            System.out.println("LoggerAspect：环绕通知->异常通知"+" 方法名："+signature.getName()+" 异常："+throwable);

        }finally {
            //后置通知
            System.out.println("LoggerAspect:环绕通知->后置通知 "+"方法名："+signature.getName()+" 执行完毕");

        }
        return result;
    }


}
